package com.jiesen.base.modules.certificate.textbook.controller;


import com.jiesen.base.baseDao.RedisServiceImpl;
import com.jiesen.base.modules.certificate.textbook.model.JsVideo;
import com.jiesen.base.modules.certificate.textbook.model.TextBook;
import com.jiesen.base.modules.certificate.textbook.service.JsVideoService;
import com.jiesen.base.modules.certificate.textbook.service.TextbookService;
import com.jiesen.base.modules.login.jsLoginUser.model.JsLoginUser;
import com.jiesen.base.utils.R;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 *
 * @author
 * @since 2020-08-27
 */
@RestController
@RequestMapping("/jsVideo")
public class JsVideoController {

    @Autowired
    private JsVideoService jsVideoService;

    @Autowired
    private RedisServiceImpl redisService;

    //增加一个视频
    @PostMapping("/create")
    @RequiresPermissions("textbook")
    public R addVideo(@RequestBody JsVideo jsVideo, HttpServletRequest request) {
        jsVideo.setCts(new Date());
        String name = jsVideo.getName();
        int i = name.lastIndexOf(".");
        if(i!=-1){
            //文件名称前缀
            String prefix = name.substring(0,i);
            //文件名称后缀
            String suffix = name.substring(i);
            jsVideo.setSuffixName(suffix);
            jsVideo.setName(prefix);
        }
        String token = request.getHeader("token");
        Object o = redisService.get(token);
        if(o==null){
            return R.error("未登录");
        }
        JsLoginUser JsLoginUser =(JsLoginUser)o;
        jsVideo.setOperator(JsLoginUser.getName());
        boolean save = jsVideoService.save(jsVideo);
        if(!save){
            return R.error("保存失败");
        }
        return R.ok().put("data",jsVideo);
    }

    //删除一个视频
    @GetMapping("/delete")
    @RequiresPermissions("textbook")
    public R deleteVideo(@RequestParam("id")Integer id){
        boolean b = jsVideoService.removeById(id);
        if(!b){
            return R.error("删除失败");
        }
        return R.ok("删除成功");
    }

    //修改一个视频
    @PostMapping("/edit")
    @RequiresPermissions("textbook")
    public R editVideo(@RequestBody JsVideo jsVideo, HttpServletRequest request){
        jsVideo.setCts(new Date());
        String name = jsVideo.getName();
        int i = name.lastIndexOf(".");
        if(i!=-1){
            //文件名称前缀
            String prefix = name.substring(0,i);
            //文件名称后缀
            String suffix = name.substring(i);
            jsVideo.setSuffixName(suffix);
            jsVideo.setName(prefix);
        }
        String token = request.getHeader("token");
        Object o = redisService.get(token);
        if(o==null){
            return R.error("未登录");
        }
        JsLoginUser JsLoginUser =(JsLoginUser)o;
        jsVideo.setOperator(JsLoginUser.getName());
        boolean b = jsVideoService.saveOrUpdate(jsVideo);
        if(!b){
            return R.error("修改失败");
        }
        return R.ok("修改成功").put("data",jsVideo);
    }

    //查询一个视频
    @GetMapping("/view")
    @RequiresPermissions("textbook")
    public R view(@RequestParam("id")Integer id){
        JsVideo jsVideo = jsVideoService.getById(id);
        if(jsVideo==null){
            return R.error("没有该数据");
        }
        return R.ok().put("data", jsVideo);
    }

    //根据条件查询视频
    @PostMapping("/view")
    @RequiresPermissions("textbook")
    public R view(@RequestBody JsVideo jsVideo){
        R page = jsVideoService.page(jsVideo);
        return page;
    }

    //根据章节id查询视频
    @GetMapping("/book/view")
    @RequiresPermissions("textbook")
    public R bookView(@RequestParam("textbookId")Integer id,@RequestParam(value = "detailed",defaultValue = "2")Integer detailds){
        List<JsVideo> jsVideos = jsVideoService.findByBookIds(id, detailds);
        if(jsVideos.size()==0 || jsVideos==null){
            return R.error("没有视频信息");
        }
        return R.ok().put("data",jsVideos);
    }

    //根据章节ids查询视频
    @PostMapping("/book/view")
    @RequiresPermissions("textbook")
    public R views(@RequestParam("ids") List<Integer> ids,@RequestParam(value = "detailed",defaultValue = "2")Integer detailds) {
        List<JsVideo> jsVideos = jsVideoService.findByBookIds(ids,detailds);
        if(jsVideos.size()==0 || jsVideos==null){
            return R.error("没有视频信息");
        }
        return R.ok().put("data",jsVideos);
    }

}